/*
 * GraphFileAccessory.java
 * Created on Jul 23, 2011, 7:23:36 AM
 */
package graphexplorer.io;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;
import java.util.TreeMap;
import javax.swing.JFileChooser;
import org.bm.blaise.scio.graph.Graph;
import org.bm.blaise.scio.graph.WeightedGraph;
import org.bm.blaise.scio.graph.io.AbstractGraphIO;
import org.bm.blaise.scio.graph.time.TimeGraph;

/**
 * Provides an accessory component for previewing the contents of a graph file.
 * @author elisha
 */
public class GraphFileAccessory extends javax.swing.JPanel implements PropertyChangeListener {

    GraphFileChooser gfc;
    File file;
    
    /** Creates new form GraphFileAccessory */
    public GraphFileAccessory(GraphFileChooser gfc) {
        this.gfc = gfc;
        initComponents();
        gfc.addPropertyChangeListener(this);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        boolean update = false;
        String prop = evt.getPropertyName();
        if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
            file = null;
            update = true;
        } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
            file = (File) evt.getNewValue();
            update = true;
        }
        
        if (update && file == null) {
            nameL.setText("Select Graph File...");
            sizeVL.setText("");
            nodesVL.setText("");
            edgesVL.setText("");
            featuresVL.setText("");
            formatVL.setText("");            
        } else if (update) {
            String nm = file.getName();
            long sz = file.length();
            nameL.setText(nm);
            sizeVL.setText(sz + " bytes");

            AbstractGraphIO loader = gfc.activeIO();
            String type = loader == null ? "unrecognized graph file type" : loader.getFileFilter().getDescription();
            formatVL.setText(type);
            
            TreeMap<Integer, double[]> loc = new TreeMap<Integer, double[]>();
            Object loaded = loader == null ? null : loader.importGraph(loc, file, null);
            boolean locs = loc != null && loc.size() > 0;
            
            if (loaded instanceof TimeGraph) {
                TimeGraph tg = (TimeGraph) loaded;
                int nv = tg.getAllNodes().size();
                nodesVL.setText(nv + (locs ? " (saved locations)" : ""));
                
                List<Double> tt = tg.getTimes();
                int ns = tt.size();
                edgesL.setText("# Time Slices:");
                edgesVL.setText(ns + "");
                
                boolean wtd = false;
                if (nv > 0) {
                    Graph slc = tg.slice((Double) tt.get(0), false);
                    if (slc instanceof WeightedGraph)
                        wtd = true;
                }
                
                boolean dir = tg.isDirected();
                featuresVL.setText("longitudinal, " + (dir ? "directed" : "undirected") + (wtd ? ", weighted" : ""));
            } else if (loaded instanceof Graph) {
                Graph g = (Graph) loaded;
                int nv = g.order();
                nodesVL.setText(nv + (locs ? " (saved locations)" : ""));
                
                int ne = g.edgeNumber();
                edgesL.setText("# Edges:");
                edgesVL.setText(ne + "");
                
                boolean dir = g.isDirected();
                boolean wtd = g instanceof WeightedGraph;
                featuresVL.setText((dir ? "directed" : "undirected") + (wtd ? ", weighted" : ""));
            } else if (loaded == null) {
                nodesVL.setText("");
                edgesVL.setText("");
                featuresVL.setText("");
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        nameL = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        formatL = new javax.swing.JLabel();
        formatVL = new javax.swing.JLabel();
        sizeL = new javax.swing.JLabel();
        sizeVL = new javax.swing.JLabel();
        nodesL = new javax.swing.JLabel();
        nodesVL = new javax.swing.JLabel();
        edgesL = new javax.swing.JLabel();
        edgesVL = new javax.swing.JLabel();
        featuresL = new javax.swing.JLabel();
        featuresVL = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        nameL.setFont(new java.awt.Font("Tahoma", 0, 18));
        nameL.setText("Select Graph File...");
        nameL.setName("nameL"); // NOI18N
        add(nameL, java.awt.BorderLayout.PAGE_START);

        jPanel1.setMinimumSize(new java.awt.Dimension(300, 200));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 200));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        formatL.setText("File Format:");
        formatL.setName("formatL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(formatL, gridBagConstraints);

        formatVL.setEnabled(false);
        formatVL.setName("formatVL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(formatVL, gridBagConstraints);

        sizeL.setText("File Size:");
        sizeL.setName("sizeL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(sizeL, gridBagConstraints);

        sizeVL.setEnabled(false);
        sizeVL.setName("sizeVL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(sizeVL, gridBagConstraints);

        nodesL.setText("# Nodes:");
        nodesL.setName("nodesL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(nodesL, gridBagConstraints);

        nodesVL.setEnabled(false);
        nodesVL.setName("nodesVL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(nodesVL, gridBagConstraints);

        edgesL.setText("# Edges:");
        edgesL.setName("edgesL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(edgesL, gridBagConstraints);

        edgesVL.setEnabled(false);
        edgesVL.setName("edgesVL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(edgesVL, gridBagConstraints);

        featuresL.setText("Features:");
        featuresL.setName("featuresL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(featuresL, gridBagConstraints);

        featuresVL.setEnabled(false);
        featuresVL.setName("featuresVL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(featuresVL, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel edgesL;
    private javax.swing.JLabel edgesVL;
    private javax.swing.JLabel featuresL;
    private javax.swing.JLabel featuresVL;
    private javax.swing.JLabel formatL;
    private javax.swing.JLabel formatVL;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel nameL;
    private javax.swing.JLabel nodesL;
    private javax.swing.JLabel nodesVL;
    private javax.swing.JLabel sizeL;
    private javax.swing.JLabel sizeVL;
    // End of variables declaration//GEN-END:variables
}
